renamePieces = #("road355","road391","road600","road528","road492","road636","road456","road391","road564")	-- need to rename them as they were a group of pieces before
--need to ungroup everything

specialsObjects = #("road9","road292","road134","road7","road301","road10","roadCurvedBridge")
forbiddenfaces = #(
	#{80},
	#{3544,3184},
	#{16818, 16827..16829, 16831..16832, 16834..16835, 16837..16838, 16840..16841, 16843..16844, 16846..16847, 16850, 16853, 16856, 21776..21779, 21980..22048, 22279, 22294..22296, 22491..22559, 28142..28143, 28145..28146, 28148..28149, 28151..28152, 28154..28155, 28157..28158, 28160..28161, 28163..28164, 34109..34112, 34313..34381, 34612, 34627..34629, 34824..34892, 38671..38672},
	#{70},
	#{148},
	#{122},
	#{21776, 21778..21779, 21980..22002, 22026..22048, 22294, 22296, 22491..22513, 22537..22559, 34109, 34111, 34313..34335, 34371..34381, 34627, 34629, 34824..34846, 34870..34880, 34890..34892, 35327, 38668, 38671..38672, 38674}
)

rotatePivots = #("road297","road296","road301","road295","road303","road304","road313","road300")
fn RotatePivotOnly obj rotation= ( local rotValInv=inverse (rotation as quat)
animate off in coordsys local obj.rotation*=RotValInv
obj.objectoffsetrot*=RotValInv
obj.objectoffsetpos*=RotValInv
)

for obj in $ do
(
	if (findItem rotatePivots obj.name) > 0 do
	(
	RotatePivotOnly obj (EulerAngles 0 0 -90)
	)
)

for obj in $ do
(
	if (findItem renamePieces obj.name) > 0 do
	(
		obj.name = "road292"
	)
)
objList = getCurrentSelection()

for obj in objList do
(
	newFaceSel = #{} --empty bitarray
	ConvertTo obj Editable_Poly

	for f = 1 to obj.numfaces where(obj.parent == undefined) do
	(
		temp =polyop.getFaceMatID obj f		
		if temp != undefined and temp != 1 and temp != 2 and temp !=4 do
		(
			append newFaceSel f
		)
	)

	if (findItem specialsObjects obj.name) > 0 then
	(
		index = findItem specialsObjects obj.name
		newFaceSel= newFaceSel - forbiddenfaces[index] -- remove forbidden faces
	)
	--polyop.deleteFaces obj newFaceSel
)


fn swapFaces obj=
(
	for f = 1 to obj.numfaces where(obj.parent == undefined) do
	(
		temp =polyop.getFaceMatID obj f
		if temp == 1 do
		(
			polyop.setFaceMatID obj f 2
		)
		if temp == 2 do
		(
			polyop.setFaceMatID obj f 1
		)
	)
)

theMat = multiMaterial numsubs:4


red_color = standard()
red_color.diffuse = color 255 0 0

blue_color = standard()
blue_color.diffuse = color 0 0 255

green_color = standard()
green_color.diffuse = color 0 255 0

theMat[1] = red_color
theMat[2] = blue_color
theMat[3] = green_color


red_color_id = 1
blue_color_id = 2
green_color_id = 3

straight2lane = #("road2","road24","road294","road3","road4","road22","road17","road296","road297","road298","road133","road305",
						"road306","road295","road299","road21","road300","road302","road303","road305","road298",
						"road307","road31","road32","road56","road33","road812","road808","road810","road811",
						 "road821","road820","road818","road813","road816","road814","road815","road817","road313","road311","road310")
specials = #("road165","road138","road166","road137","road292","road24","road127","road21","road10","road128",
					"road2","road298","road66","roadCurvedBridge","ramp","road88","road9","road7","road301","road304","road134")--has to be in same order as appearance in red/blue_pieces arrays
	red_pieces = #(
	#{1, 4, 6, 8, 10, 12, 14, 16},
	#{2, 4, 6, 8, 10, 12},
	#{2, 4..5, 7, 9, 11},
	#{1, 4, 6, 8, 10, 12, 14, 16},
	#{1..2, 7..8, 10..11, 14..15, 17..19, 24..27, 30..31, 33},
	#{1},
	#{1..30},
	#{2},
	#{2,3,5},
	#{91..180},
	#{1},
	#{1},
	#{1..22, 67..110, 133..176, 265..281, 282..286, 287..352},
	#{1..2, 5..17, 24..28, 35..51, 75..99, 101..147, 171..194, 196..218, 242, 244..266, 290, 292..314, 338, 340..362, 386..387, 389..423, 447..471, 473..510, 534..556, 559..560, 563..565},
	#{},
	#{20, 35, 56},
	#{2,3,5},
	#{1,3,5},
	#{1,3,5},
	#{1},
	#{20, 22..23, 25..48, 72..94, 118..142, 144, 146..169, 193..215, 239..262, 264..286, 310, 312..334, 358..374, 376..398, 422, 424..446, 470, 472..473, 475..498, 522..544, 568..592, 594, 596..619, 643..665, 689..711, 713..714, 716..717}
	-- These are the selections for 4laneOverpass #{20, 22..23, 25..48, 72..94, 118..142, 144, 146..169, 193..215, 239..262, 264..286, 310, 312..334, 374, 376..398, 422, 424..446, 470, 472..473, 475..498, 522..544, 568..592, 594, 596..619, 643..665, 689..714, 716..717}
	)
	blue_pieces = #(
	#{2,3, 5, 7, 9, 11, 13, 15},
	#{1,3,5, 7, 9, 11},
	#{1, 3, 6, 8, 10, 12},
	#{2,3, 5, 7, 9, 11, 13, 15},
	#{3..6, 9, 12..13, 16, 20..23, 28..29, 32, 34},
	#{2},
	#{31..60},
	#{1},
	#{1,4},
	#{1..90},
	#{2},
	#{2},
	#{23..34, 35..66, 111..132, 177..264, 353..388, 389..396},
	#{4, 52..74, 100, 148..170, 195, 219..241, 243, 267..289, 291, 315..337, 339, 363..385, 388, 424..446, 472, 511..533, 557..558, 561..562,3, 18..23, 29..34},
	#{1..252},
	#{1..19, 21..34, 36..55,57..69},
	#{1,4},
	#{2,4},
	#{2,4},
	#{2},
	#{3, 5, 7, 9, 11, 13, 15, 24, 95..117, 145, 216..238, 263, 287..309, 311, 335..358, 360, 362, 364, 366, 368, 370, 372, 375, 399..421, 423, 447..469, 474, 545..567, 595, 666..688, 712, 715}
	-- These are the selections for 4laneOverpass #{24, 95..117, 145, 216..238, 263, 287..309, 311, 335..357, 375, 399..421, 423, 447..469, 474, 545..567, 595, 666..688, 712, 715}
	)
	--making use of array of bitarrays which gains us performance and allows us to make listing more succinct for each piece

	

for obj in $ where(obj.parent == undefined) do
(
	mediancolors = #{}
		for f = 1 to obj.numfaces do
		(
		temp = polyop.getFaceMatID obj f
			if (temp == 8 OR temp == 9) then
			append mediancolors f
		)
	if (findItem straight2lane obj.name) > 0 do
	(
		for f = 1 to obj.numfaces do
		(
			tempPoint = polyop.getFaceCenter obj f
			--tempPoint = tempPoint * inverse obj.transform

			if  abs obj.rotation.z_rotation == 0 OR abs (obj.rotation.z_rotation + 180) < 1 OR abs (-obj.rotation.z_rotation + 180) < 1 do
			(
				obj.material = theMat
				if tempPoint.Y > obj.center.Y then
				(
				polyop.setFaceMatID obj f blue_color_id
				)
				else
				(
				polyop.setFaceMatID obj f red_color_id
				)
			)

			if  abs(90 - obj.rotation.z_rotation) < 1  OR abs(90 + obj.rotation.z_rotation) < 1 do
			(
				obj.material = theMat
				if tempPoint.X > obj.center.X then
				(
				polyop.setFaceMatID obj f blue_color_id
				)
				else
				(
				polyop.setFaceMatID obj f red_color_id
				)
			)
		)
	)			
	
	if (findItem specials obj.name) > 0 do
	(
		
		index = findItem specials obj.name
		for f = 1 to obj.numfaces do
		(
			polyop.setFaceMatID obj f green_color_id
		)
			polyop.setFaceMatID obj red_pieces[index] red_color_id
			polyop.setFaceMatID obj blue_pieces[index] blue_color_id		
		obj.material = theMat
	)
	
	if(obj.name == "road166" ) do
	(
		rotationAngles = obj.rotation as eulerAngles
		
		if  abs rotationAngles.z_rotation == 0  do
		(
			polyop.setFaceMatID obj #{6, 8, 10, 12} blue_color_id
			polyop.setFaceMatID obj #{5, 7, 9, 11} red_color_id
		)
		if  abs rotationAngles.z_rotation == 180.0  do
		(
			swapFaces obj
		)
		if  rotationAngles.z_rotation > 89.0 and rotationAngles.z_rotation < 91.0  do
		(
			polyop.setFaceMatID obj #{2,4} blue_color_id
			polyop.setFaceMatID obj #{1,3} red_color_id
		)
	)
	if(obj.name == "road138") do
	(
		rotationAngles = obj.rotation as eulerAngles
		if  rotationAngles.z_rotation > 179.0 and rotationAngles.z_rotation < 181.0 do
		(
			polyop.setFaceMatID obj #{6, 8, 10, 12} blue_color_id
			polyop.setFaceMatID obj #{5, 7, 9, 11} red_color_id
		)
		if  rotationAngles.z_rotation > 89.0 and rotationAngles.z_rotation < 91.0  do
		(
			polyop.setFaceMatID obj #{2,4} red_color_id
			polyop.setFaceMatID obj #{1,3} blue_color_id
		)
	)
		if(obj.name == "road127") do
	(
		rotationAngles = obj.rotation as eulerAngles
		if  abs(rotationAngles.z_rotation) > 27 AND  abs(rotationAngles.z_rotation) < 31 do
		(
			swapFaces obj
		)
		if  abs(rotationAngles.z_rotation) < 1 do
		(
			swapFaces obj
		)
	)

polyop.setFaceMatID obj mediancolors red_color_id
)
